package com.commons;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginStatusFilter implements Filter {
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();

		String isLogin =  (String) session.getAttribute("logined");
		String requestURI = ((HttpServletRequest) request).getRequestURI();
		System.out.println("请求路径为："+ requestURI);
		if(requestURI.equals("/CompanyVisitorsSystem/login.jsp")||requestURI.equals("/CompanyVisitorsSystem/login")) {
			chain.doFilter(request, response);		
		}		
		else{
			if ("no".equals(isLogin) || null ==isLogin) {
				System.out.println("小贼，别想过我这关！");
				response.sendRedirect(request.getContextPath() + "/login.jsp");
			} else if("yes".equals(isLogin)) {
				System.out.println("欢迎你！");
				chain.doFilter(request, response);
			}
		}
	}

	public void destroy() {

	}
	public void init(FilterConfig filterConfig) throws ServletException {

	}
}
